package com.xungou.resource.service.impl;

import com.xungou.core.mvc.AbstractService;
import com.xungou.core.mvc.IDao;
import com.xungou.resource.dao.IResource;
import com.xungou.resource.service.IResourceService;
import org.springframework.stereotype.Service;
import org.springframework.web.context.ContextLoader;

import javax.annotation.Resource;
import java.io.File;


@Service("ResourceService")
public class ResourceServiceImpl extends AbstractService<com.xungou.resource.pojo.Resource> implements IResourceService {
    @Resource
    private IResource iResource;

    @Override
    protected IDao<com.xungou.resource.pojo.Resource> getDao() {
        return iResource;
    }

    @Override
    public int deleteByPrimaryKey(Integer id) {
        com.xungou.resource.pojo.Resource resource = getDao().selectByPrimaryKey(id);
        String webRealRoot = ContextLoader.getCurrentWebApplicationContext().getServletContext().getRealPath("/");
        String path=webRealRoot  + File.separator +resource.getPath();
        File file=new File(path);
        // 路径为文件且不为空则进行删除
        if (file.isFile() && file.exists()) {
            file.delete();
        }
        return super.deleteByPrimaryKey(id);
    }
}